package com.carepad.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.carepad.entity.Users;
import com.carepad.service.UsersService;
import com.carepad.dao.UsersMapper;

@Service("usersService")
public class UsersServiceImpl implements UsersService{

	@Autowired
	private UsersMapper usersMapper;
	
	@Override
	public Users signIn(String username, String password) {

		List<Users> list = usersMapper.findByUsernameAndPassword(username, password);

		if (list == null) {
			// error
		} else if (list.size() == 0) {
			// user not found
		} else if (list.size() > 1) {
			// too many users
		} else {
			Calendar calendar = Calendar.getInstance();
			list.get(0).setDateUpdated(calendar.getTime());
			return list.get(0);
		}

		return null;
	}

	@Override
	public Users findUsersByEmail(String email) {
		
		List<Users> list = usersMapper.findByEmailAddress(email);

		if (list == null || list.size() == 0) {
			return null;
		} else if (list.size() == 1) {
			return list.get(0);
		} else {
			// error
			return null;
		}
	}
}
